package com.wenwei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wenwei.adapter.CouponAdapter;
import com.wenwei.bean.AllCouponBean;
import com.wenwei.bean.UserCouponBean;
import com.wenwei.net.ApiManager;
import com.wenwei.net.BaseSubscriber;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.UserStringUtils;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/22.
 */
public class CouponActivity extends BaseActivity implements CouponAdapter.CallBack {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.recycler)
    XRecyclerView recycler;
    @Bind(R.id.header)
    CardView cardView;

    CouponAdapter mAdapter;

    @Bind(R.id.empty)
    View mView;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_coupon;
    }

    private int mType;
    private ArrayList<UserCouponBean> mList;

    @Override
    protected void init() {
        super.init();
        mType = getIntent().getIntExtra("type", 0);
        if (mType == Constant.COUPON_USER) {
            mList = (ArrayList<UserCouponBean>) getIntent().getSerializableExtra("bean");
        }
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("优惠券");
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        recycler.setLoadingMoreEnabled(false);
        recycler.setEmptyView(mView);
        if (getIntent().getIntExtra("state", 0) == 1) {
            recycler.setPullRefreshEnabled(false);
            cardView.setVisibility(View.VISIBLE);
        }
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (mType == Constant.COUPON_USER) {
                    getUserCoupon();
                } else {
                    getCoupon();
                }

            }

            @Override
            public void onLoadMore() {

            }
        });
        if (mType == Constant.COUPON_USER) {
            mAdapter = new CouponAdapter(mType, getIntent().getIntExtra("state", 0));
            if (mList == null) {
                getUserCoupon();
            }
            mAdapter.setmUserList(mList);
            mAdapter.setCallBack(this);
            recycler.setAdapter(mAdapter);
        } else {
            mAdapter = new CouponAdapter(mType, 0);
            mAdapter.setCallBack(this);
            recycler.setAdapter(mAdapter);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void initData() {
        super.initData();
        if (mType == Constant.COUPON_ALL) {
            getCoupon();
        }else if (mType == Constant.COUPON_USER){
            getUserCoupon();
        }
    }


    //所有优惠券列表
    private BaseSubscriber<ArrayList<AllCouponBean>> couponSb;

    private void getCoupon() {
        couponSb = new BaseSubscriber<ArrayList<AllCouponBean>>(mContext) {
            @Override
            public void onSuccess(ArrayList<AllCouponBean> allCouponBeen) {
                mAdapter.setmALlList(allCouponBeen);
                mAdapter.notifyDataSetChanged();
                recycler.refreshComplete();
            }
        };
        ApiManager.getInstance().getCoupon(couponSb, UserStringUtils.getUserId(mContext));
    }

    //领取优惠券
    private BaseSubscriber<Object> addSb;

    private void addUserCoupon(int couponId) {
        addSb = new BaseSubscriber<Object>(mContext, true, "请稍后") {
            @Override
            public void onSuccess(Object o) {
                showToast("领取成功");
                getCoupon();
            }
        };
        try {
            JSONObject jb = new JSONObject();
            jb.put("couponId", couponId);
            jb.put("userId", UserStringUtils.getUserId(mContext));
            ApiManager.getInstance().addUserCoupon(addSb, jb.toString());
        } catch (Exception e) {

        }
    }

    @Override
    public void onItemClick(int couponId) {
        //领取优惠券
        addUserCoupon(couponId);
    }

    @Override
    public void onUseCoupon(UserCouponBean bean) {
        //使用优惠券
        Bundle bundle = new Bundle();
        Intent intent = new Intent();
        bundle.putSerializable("bean", bean);
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }


    //用户优惠券
    private BaseSubscriber<ArrayList<UserCouponBean>> couponSbbb;

    private void getUserCoupon() {
        couponSbbb = new BaseSubscriber<ArrayList<UserCouponBean>>(mContext) {
            @Override
            public void onSuccess(ArrayList<UserCouponBean> userCouponBeen) {
                mList = userCouponBeen;
                mAdapter.setmUserList(mList);
                mAdapter.notifyDataSetChanged();
                recycler.refreshComplete();
            }
        };
        ApiManager.getInstance().getUserCoupon(couponSbbb, UserStringUtils.getUserId(mContext));
    }

    @OnClick({R.id.base_back, R.id.coupon_used})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.coupon_used:
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putSerializable("bean", null);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }
}
